
// 本hook的功能是获取天气数据
// https://devapi.qweather.com/v7/weather/now?location=经纬度&key=你的key
// CC-- 导入pinia的两个方法
import { storeToRefs } from "pinia";
import {CStore} from '../../stores/counter'
import { ref, computed } from 'vue'

// CC-- 解构出pinia中存储的数据
const $store = CStore()
export const getWeatherData = async(lnglat = [114.30, 30.50]) => {
    console.log('参数',lnglat);
    const CWeather = ref({}) 
    const key = '13fdbee456fc4187a685ae69f5194bbc'
     fetch(
            `https://devapi.qweather.com/v7/weather/now?location=${lnglat}&key=${key}`,
            {
              type: "get",
              dataType: "json",
            }
          )
            .then((response) => {
              if (response.ok) return response.json()
            })
            .then((data) => {
            console.log(data,"data数据");
        $store.setWeatherDate(
                data.now.temp,
                data.now.icon,
                data.now.text,
                data.now.windDir,
                data.now.windSpeed,
                data.now.windScale,
                data.now.humidity,
            )
            localStorage.setItem("weather", JSON.stringify($store.cityWeatherDate));
            CWeather.value = $store.cityWeatherDate
            
        })

        return {CWeather }
}


 